package com.rede.didiok.rank.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.common.utils.PageUtils;
import com.rede.didiok.common.utils.Query;
import com.rede.didiok.rank.dao.AttrValueDao;
import com.rede.didiok.rank.entity.AttrValueEntity;
import com.rede.didiok.rank.service.AttrValueService;


@Service("attrValueService")
public class AttrValueServiceImpl extends ServiceImpl<AttrValueDao, AttrValueEntity> implements AttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrValueEntity> page = this.page(
                new Query<AttrValueEntity>().getPage(params),
                new QueryWrapper<AttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrValueEntity> listAttrByMemeberId(String memberUid) {
        List<AttrValueEntity> entities = this.baseMapper.selectList(new QueryWrapper<AttrValueEntity>().eq("member_uid", memberUid));
        return entities;
    }

    /**
     * 保存榜单成员的属性
     *
     * @param collect
     */
    @Override
    public void saveRankMemberAttr(List<AttrValueEntity> collect) {
        this.saveBatch(collect);
    }

    /**
     * 根据 memberId 查询榜单成员的属性
     *
     * @param memberUid
     * @return
     */
    @Override
    public List<AttrValueEntity> listForMember(String memberUid) {
        List<AttrValueEntity> entities = this.baseMapper.selectList(new QueryWrapper<AttrValueEntity>().eq("member_uid", memberUid));
        return entities;
    }

    /**
     * 根据榜单成员id修改榜单成员的属性
     *
     * @param memberUid
     * @param entities
     */
    @Override
    public void updateRankMemberAttr(String memberUid, List<AttrValueEntity> entities) {

        //1、删除这个memberId之前对应的所有属性
        this.baseMapper.delete(new QueryWrapper<AttrValueEntity>().eq("member_uid",memberUid));


        List<AttrValueEntity> collect = entities.stream().map(item -> {
            item.setMemberUid(memberUid);
            return item;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }
}